package cn.pud.exceltool.Utils;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Author: Miyako
 * @CreateTime: 2024-11-14  20:02
 * @Description: 输入工具类
 */

public class InsertUtil {

    //A表字段集合
    private Set<String> setA = new LinkedHashSet<>();
    //B表字段集合
    private Set<String> setB = new LinkedHashSet<>();
    //B与A的差集
    private Set<String> difSet ;
    //A与B的并集
    private Set<String> unSet ;
    //A与B的交集
    public Set<String> inSet ;

    public InsertUtil(){

    }

    public InsertUtil(String tableA , String tableB ,String regex){
        String[] tA = importData(tableA, regex);
        String[] tB = importData(tableB, regex);
        arrToSet(tA,tB);
        getDifSet();
        getUnSet();
        getInSet();
    }
    
        /**
         * @description: 将字段名字符串装入字符串数组
         * @param fieldNames 字段名字符串
         * @param regex (正则表达式)自定义分隔符 默认使用换行符隔开
         * @return: 字段名数组
         **/
    public String[] importData (String fieldNames , String regex){
        String [] fieldArr = null;
        if (regex == null){
            //可以匹配win和linux的换行符
            fieldArr = fieldNames.split("\r?\n");
        }else {
            //自定义分隔符规则
            fieldArr = fieldNames.split(regex);
        }
        return fieldArr;
    }

    /**
     * @description: 将命名规则换位驼峰命名规则 example: USER_POWER_VERIFY -> userPowerVerify
     * @param fieldName 适配用"_"分隔的命名方式
     * @return:  驼峰命名的数据
     **/
    private String  changeToHumpNaming(String fieldName){

        StringBuilder result = new StringBuilder();
            //将单词分开
            String[] split = fieldName.split("_");
            //首个单词全小写
            result.append(split[0].substring(0).toLowerCase());
            for (int s = 1; s < split.length; s++) {
                //首字母大写
                result.append(split[s].substring(0, 1).toUpperCase());
                //其余字母小写
                result.append(split[s].substring(1).toLowerCase());
            }




        return result.toString();
    }
    //获取驼峰命名字符串
    public String getHumpStr(){
        StringBuilder result = new StringBuilder();
        for (String item : unSet){
            String humpStr = changeToHumpNaming(item);
            result.append(humpStr)
                    .append(",");
        }
        return result.toString();
    }

    //不需要驼峰命名的普通字段属性名调用
    public String getUnionSet(){
        StringBuilder result = new StringBuilder();
        for (String item: unSet){
            result.append(item)
                    .append(",");
        }
        return result.toString().replaceAll("\n?\r","");
    }


    /**
     * @description: 循环数据需要添加样式
     * @author: Miyako
     * @param: [prefix 前缀, suffix 后缀]
     * @return: java.lang.String
     **/
    public String getLoopFormat(String prefix , String suffix){
        //先转驼峰命名
        String allHumpStr = getHumpStr();

        StringBuilder result = new StringBuilder();

        String[] filedArr = importData(allHumpStr, ",");
        for (String item : filedArr) {
            result.append(prefix)
                    .append(item)
                    .append(suffix)
                    .append(",");
        }
        return result.toString().replaceAll("\n?\r","");
    }
    
    /**
     * @description: 多表联查使用,两个表的字段求并集,并上添加别名  example: a.id  b.username
     * @param:  aNickname:父表别名     bNickname:子表别名
     * @return: 添加别名且字段名数组不重复
     **/
    public String addNickname(String aNickname  ,String bNickname ){

        StringBuilder result = new StringBuilder();
        //添加别名
        for (String itemA : this.setA){
         result.append(aNickname)
                 .append(".")
                 .append(itemA)
                 .append(",")
                 ;
        }

        for (String itemB : this.difSet){
            result.append(bNickname)
                    .append(".")
                    .append(itemB)
                    .append(",")
                    ;
        }

        return result.toString();
    }




    /**
     * @description:数组存入集合
     * @return: void
     **/
    private void arrToSet(String[] tableA,String[] tableB){
        //将数据加入set集合中去重
        for (String itemA : tableA)
        this.setA.add(itemA);
        for (String itemB : tableB)
        this.setB.add(itemB);
    }

    //获取子表与父表的差集
    private void getDifSet(){
        difSet = new LinkedHashSet<>(setB);
        difSet.removeAll(this.setA);
    }
    //获取并集
    private void getUnSet(){
        unSet = new LinkedHashSet<>(setA);
        for (String item : difSet)
        unSet.add(item);
    }

    private void getInSet(){
        inSet = new LinkedHashSet<>(setB);
        inSet.removeAll(this.difSet);
    }




}
